<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="设施类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="facilityType">
              <!-- <a-input v-model="model.facilityName" placeholder="请输入设施名称"  ></a-input> -->
              <j-dict-select-tag v-model="model.facilityType" dictCode="nw_terminal_facility">
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设施状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-select v-model="model.status">
                <a-select-option :value="1">正常</a-select-option>
                <a-select-option :value="2">异常</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设施编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="facilityNo">
              <a-input v-model="model.facilityNo" placeholder="请输入设施编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="终端选择" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="terminalId">
              <a-select
                v-model="model.terminalId"
                show-search
                allowClear
                placeholder="请选择"
                :default-active-first-option="false"
                :show-arrow="false"
                :not-found-content="null"
                option-filter-prop="children"
                @change="handleTerminalSelected"
              >
                <!-- 显示终端名称+编号 -->
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="item in terminalList" :key="item.value" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="终端名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="terminalName">-->
<!--              <a-input v-model="model.terminalName" placeholder="请输入终端名称"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="关联企业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enterprise">
              <a-input v-model="model.enterprise" placeholder="关联企业" disabled></a-input>
<!--              <a-select-->
<!--                v-model="model.enterprise"-->
<!--                placeholder="请选择企业"-->
<!--                style="width: 100%"-->
<!--                @change="handleChange"-->
<!--              >-->
<!--                <a-select-option :value="undefined">请选择</a-select-option>-->
<!--                <a-select-option v-for="item in enterpriseList" :key="item.id" :value="item.id">-->
<!--                  {{ item.enterpriseName }}-->
<!--                </a-select-option>-->
<!--              </a-select>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="enterprise">
            <a-form-model-item label="运维人员" :labelCol="labelCol" :wrapperCol="wrapperCol" class="ant-form-item-required" prop="operator">
              <a-select
                v-model="opList2"
                mode="multiple"
                placeholder="请选择运维人员"
                style="width: 100%"
                @change="handleChange3"
              >
                <a-select-option v-for="item in operatorList" :key="item.id" :value="item.id">
                  {{ item.operatorName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'TerminalFacilityForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        terminalList:[],
        operatorList:[],
        opList2:[],
        enterpriseList:[],
        enterprise:{},
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/equ1/terminalFacility/add",
          edit: "/equ1/terminalFacility/edit",
          queryById: "/equ1/terminalFacility/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getTerminalList()
    },
    methods: {
      // async getEnterpriseList() {
      //   const res = await getAction(`/ps/enterprise/list`)
      //   if (res.success) {
      //     this.enterpriseList= res.result.records
      //     // this.enValue=res.result.records[0].id
      //   }
      // },
      async getEnterprise(id) {
        const res = await getAction(`/terminal/terminal/getEnterpriseByTerminalId`,{terminalId:id})
        if (res.success) {
          this.enterprise= res.result
          this.model.enterprise=res.result.enterpriseName
          this.opList2=[]
          this.getOperatorIds( res.result.id)
          // this.enValue=res.result.records[0].id
        }
      },
      handleChange3(e){
        console.log(e,'2222222222222')
        this.opList2= e
      },
      async getOperatorIds(id) {
        const res = await getAction(`/ps/operator/list`, { enterpriseId:id })
        if (res.success) {
          this.operatorList= res.result.records
        }
      },
      handleTerminalSelected(e) {
        console.log(e,'9.44444444444')
        this.getEnterprise(e)
      },
      async getTerminalList() {
        try {
          const res = await getAction(`/terminal/terminal/list`, { pageNo: 1, pageSize: 10000 })
          const terminalList = res.result.records || []
          this.terminalList = terminalList.map((terminal) => ({
            value: terminal.terminalId,
            label: `${terminal.terminalName}(${terminal.terminalId})`,
          }))
        } catch (error) {
          console.log(error)
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.model.enterprise=record.enterpriseLabel
        this.visible = true;
        this.opList2=record.operator.split(",");
        this.getOperatorIds(record.enterprise)

        ;
      },
      submitForm () {
        const that = this;
        const ids2=this.opList2.join(',')
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            const params={...this.model,operator:ids2,enterprise:this.enterprise.id}
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>